<template>
  <!--banner轮播-->
  <div class="swiper-container" ref="mySwiper">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="item in bannerList" :key="item.id">
        <img :src="item.image_src" />
      </div>
    </div>
    <!-- 分页器 -->
    <div class="swiper-pagination"></div>
    <!-- 导航按钮 -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import Swiper from "swiper";

export default {
  name: "Swiper",
  computed: {
    ...mapState("home", ["bannerList"]),
  },
  watch: {
    //监听bannerList列表变化
    bannerList: {
      handler() {
        this.$nextTick(() => {
          //swiper轮播图
          new Swiper(this.$refs.mySwiper, {
            autoplay: true, //开启自动轮播
            loop: true, // 开启循环模式选项
            pagination: {
              el: ".swiper-pagination", // 分页器
              clickable: true,
            },
            navigation: {
              nextEl: ".swiper-button-next", // 前进后退按钮
              prevEl: ".swiper-button-prev",
            },
          });
        });
      },
    },
  },
  mounted() {
    this.$store.dispatch("home/getBannerList");
  },
};
</script>

<style scoped lang="less">
</style>